﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace PictureProcessing.Comm
{
    public class DirFile
    {
        #region 检测指定目录是否存在
        /// <summary>
        /// 检测指定目录是否存在
        /// </summary>
        /// <param name="directoryPath">目录的绝对路径</param>
        /// <returns></returns>
        public static bool IsExistDirectory(string directoryPath)
        {
            return Directory.Exists(directoryPath);
        }
        #endregion

        #region 检测指定文件是否存在,如果存在返回true
        /// <summary>
        /// 检测指定文件是否存在,如果存在则返回true。
        /// </summary>
        /// <param name="filePath">文件的绝对路径</param>        
        public static bool IsExistFile(string filePath)
        {
            return File.Exists(filePath);
        }
        #endregion

        public static string CreateDir(string dir, string dirname)
        {
            string newPath = "";
            newPath = System.IO.Path.Combine(dir, dirname);
            if (!IsExistDirectory(newPath))
            {
                System.IO.Directory.CreateDirectory(newPath);
            }
            return newPath;
        }

        /// <summary>
        /// 移动文件(剪贴--粘贴)
        /// </summary>
        /// <param name="dir1">要移动的文件的路径及全名(包括后缀)</param>
        /// <param name="dir2">文件移动到新的位置,并指定新的文件名</param>
        public static void MoveFile(string dir1, string dir2)
        {
            if (File.Exists(dir1))
            File.Move(dir1,dir2);
          
        }

        /// <summary>
        /// 文件重命名
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="OldName"></param>
        /// <param name="NewName"></param>
        public static void Rename(string OldName,string NewName) 
        {

            MoveFile(OldName,NewName);
            File.Delete(OldName);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strSaveRoute">保存路径</param>
        /// <param name="fullname">文件名称（不含路径）</param>
        /// <param name="fullPath">要移动的文件的路径及全名(包括后缀)</param>
        /// <returns></returns>
        public static void Sorting(string strSaveRoute, string fullname, string fullPath)
        {
            //bool result = true;
            //保存目录全路径
            string newPath = System.IO.Path.Combine(strSaveRoute, fullname.Remove(fullname.LastIndexOf(".")));
            string newname = System.IO.Path.Combine(newPath, fullname);
            try
            {
                //1.先判断文件夹是否存在
                if (!IsExistDirectory(newPath))
                {
                    System.IO.Directory.CreateDirectory(newPath);
                    //把文件移动创建的目录下
                    MoveFile(fullPath, newname);
                }
                else
                {
                    //把文件移动创建的目录下
                    MoveFile(fullPath, newname);
                }



            }
            catch (Exception ex)
            {

                throw;
            }



            //  return result;

        }


    }
}
